import QtQuick 2.2
import QtMultimedia 5.2

Image {
    id: sfu
    source: "LIFT/ui2/bk.png"
    width: 768
    height: 1024
    signal start;
    signal tout;
    signal keypress(int key)
    property bool jflag:false
    property bool isLongHold:false

    property int  regnum:0

    property var flrn:["-1","1","2","3","4","5","6","7","8"]
    property var voicen:["V-1","V01","V02","V03","V04","V05","V06","V07","V08"]

    property var ste:["","检修","消防","锁梯","超载","专用","故障","司机","满载"];//"A","F"
    property var ste_en:["","INSPECTION","FIRE","LOCK","OVERLOAD","INDEPENDENT","FAULT","ATTENDENT","FULL"];

//    MouseArea {
//        anchors.fill: parent
//        anchors.centerIn: parent
//        onClicked: {
//            sfu.start();
//        }
//    }

    Item {
        id:dirItem
        clip:true
        x:214
        y:(stetem1.text=="")?188 : 188-steItem.height/2
        width: 82
        height: 64
        Image{
            id:dirItem1
            anchors.centerIn: parent
            source: ""
        }
    }

    Item {
        id:flrItem
        clip:true
        x:192
        y:(stetem1.text=="")?281 : 281-steItem.height/2
        width: 123
        height: 105
        Image{
            id:flrItem1
            anchors.centerIn: parent
            source: ""
        }
    }

    Item {
        id:steItem
        //clip:true
        x:190
        y:(stetem1.text=="")?400 : 400-steItem.height/2
        width: 123
        height: 56
        Text{
            id:stetem1
            anchors.centerIn: parent
            font.family: "WenQuanYi Micro Hei Mono"
            font.pixelSize: text.length > 7 ? parent.height * .5 : parent.height * .8
            //style: Text.Sunken;
            //color: "#4C2500";
            color: "red";
            //styleColor: "black";
            smooth: true
        }
    }


    Item {
        id:flrItemS
        clip:true
        x:510
        y:376
        width: 55
        height: 46
        Image{
            id:flrItemS1
            anchors.centerIn: parent
            source: ""
        }
    }

    Item {
        id:flrItemR
        clip:true
        x:355
        y:206
        width: 255
        height: 91

        Item {
            id:flrItemRtmp
            clip:false
            x:0
            y:0
            width: regnum*106
            height: 91

        NumberAnimation on x {
            id: txtscroll
            running : false
            from: flrItemR.width
            to: 0-flrItemRtmp.width
            duration: flrItemRtmp.width*10
            loops: Animation.Infinite
        }

        Image{
            id:flrItemR1
            anchors.left: parent.left
            //source: "LIFT/ui2/ML/-1.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR2
            anchors.left: flrItemR1.right
            //source: "LIFT/ui2/ML/1.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR3
            anchors.left: flrItemR2.right
           // source: "LIFT/ui2/ML/2.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
            border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR4
            anchors.left: flrItemR3.right
           // source: "LIFT/ui2/ML/3.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR5
            anchors.left: flrItemR4.right
           // source: "LIFT/ui2/ML/4.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR6
            anchors.left: flrItemR5.right
           // source: "LIFT/ui2/ML/5.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

        Image{
            id:flrItemR7
           anchors.left: flrItemR6.right
            //source: "LIFT/ui2/ML/6.png"
           Rectangle{
           anchors.fill: parent
           anchors.topMargin: -3
           anchors.bottomMargin: -3
           border.color: "#755A30"
          border.width: 3
           color: "#CAB59A"
           opacity: .0
           }
        }

        Image{
            id:flrItemR8
            anchors.left: flrItemR7.right
            //source: "LIFT/ui2/ML/7.png"
            Rectangle{
            anchors.fill: parent
            anchors.topMargin: -3
            anchors.bottomMargin: -3
            border.color: "#755A30"
           border.width: 3
            color: "#CAB59A"
            opacity: .0
            }
        }

      }
    }

    Item{
        x:156
        y:590
        width: 459
        height: 64
        Row {
            id: row1
            anchors { fill: parent; topMargin: 0; leftMargin: 0; }
            spacing: 15

            Button{width: 64;height: 64;text: "G";source0: "LIFT/ui2/close.png";source1: "LIFT/ui2/close_dn.png";onBtclicked:{keypress(10);btreply(str)}}
            Button{width: 64;height: 64;text: "K";source0: "LIFT/ui2/open.png";source1: "LIFT/ui2/open_dn.png";onBtclicked:{keypress(9);btreply(str)}}
            Button{id:btn1;width: 64;height: 64;text: "-1";source0: "LIFT/ui2/-1.png";source1: "LIFT/ui2/-1_dn.png";onBtclicked:{keypress(1);btreply(str)}}
            Button{id:btn2;width: 64;height: 64;text: "1";source0: "LIFT/ui2/1.png";source1: "LIFT/ui2/1_dn.png";onBtclicked:{keypress(2);btreply(str)}}
            Button{id:btn3;width: 64;height: 64;text: "2";source0: "LIFT/ui2/2.png";source1: "LIFT/ui2/2_dn.png";onBtclicked:{keypress(3);btreply(str)}}
            Button{id:btn4;width: 64;height: 64;text: "3";source0: "LIFT/ui2/3.png";source1: "LIFT/ui2/3_dn.png";onBtclicked:{keypress(4);btreply(str)}}
        }
    }

        Item{
            x:156
            y:672
            width: 459
            height: 64
            Row {
                id: row2
                anchors { fill: parent; topMargin: 0; leftMargin: 0; }
                spacing: 15

                //Button{width: 64;height: 64;text: "J";source0: "LIFT/ui2/bell.png";source1: "LIFT/ui2/bell_dn.png";onBtclicked:{btreply(str)}}
                Image {
                    id: bell
                    source: "LIFT/ui2/bell.png"
                    MouseArea {
                        id: mouseArea1
                        anchors.fill: parent

                        onClicked:{
                            root.bellen = true
                            if(root.jflag)
                            {
                                bell.source="LIFT/ui2/bell.png"
                                keypress(16)
                            }
                            else
                            {
                                bell.source= "LIFT/ui2/bell_dn.png"
                                keypress(15);
                            }
                            //root.jflag = !root.jflag
                            timerdl2.start()
                            btreply("J")
                        }

                    }
                }
                Button{width: 64;height: 64;text: "S";source0: "LIFT/ui2/set.png";source1: "LIFT/ui2/set_dn.png";onBtclicked:{btreply(str)}}
                Button{id:btn5;width: 64;height: 64;text: "4";source0: "LIFT/ui2/4.png";source1: "LIFT/ui2/4_dn.png";onBtclicked:{keypress(5);btreply(str)}}
                Button{id:btn6;width: 64;height: 64;text: "5";source0: "LIFT/ui2/5.png";source1: "LIFT/ui2/5_dn.png";onBtclicked:{keypress(6);btreply(str)}}
                Button{id:btn7;width: 64;height: 64;text: "6";source0: "LIFT/ui2/6.png";source1: "LIFT/ui2/6_dn.png";onBtclicked:{keypress(7);btreply(str)}}
                Button{id:btn8;width: 64;height: 64;text: "7";source0: "LIFT/ui2/7.png";source1: "LIFT/ui2/7_dn.png";onBtclicked:{keypress(8);btreply(str)}}

            }

        }

        Item{
            id:mainpage
            x:110
            y:60
            width: 546
            height: 746
            visible: false
            Dialog{
                id:dialog
                onLogout: {mainpage.visible=false;timer.start()}
            }
        }

       Item{
            x:112
            y:832
            width: 544
            height: 140

            Timer
            {
                id:timer1
                interval: 5000
                running: false
                repeat: false;
                onTriggered: {
                    isLongHold = true
                    timer.stop();
                    //Qt.createComponent("Dialog.qml").createObject(mainpage, { });
                   dialog.loadcfg();
                   mainpage.visible=true
                }
            }

            MouseArea {
                id:longps
                anchors.fill: parent
                onPressed: {
                    //btreply("");
                    timer1.start()
                }

                onReleased:
                {
                    timer1.stop()
                    if(isLongHold)
                    {
                        console.log("long hold")
                    }
                    else
                    {
                        console.log("not long hold")
                    }
                    isLongHold = false
                }
            }
        }

       Item {
           id:voiceItem
           MediaPlayer {
               id: mediaplayer
               source: ""
               volume:dialog.defvol*.1
               //loops: Audio.Infinite
           }

           VideoOutput {
               source: mediaplayer
           }
       }

       Timer {
            id: timerdl2;
            interval: 500;
            running: false;
            repeat: false;
            triggeredOnStart: false;
            onTriggered:{
                   root.bellen = false
                }
            }


        function btreply(str){

            timer.stop();
            timer.start();

            if(str==="")
                 return
            else if(str==="S")
            {
                timer.stop();
                sfu.start();
            }
            else if(str==="J")
                return
            else if(str==="K")
                return
            else if(str==="G")
                return
            else
            {
                flrItemS1.source="LIFT/ui2/SL/"+str+".png"
                timers.start();
            }
        }

        Timer
        {
            id:timers
            interval: 1000
            running: false
            repeat: false;
            onTriggered: {
                flrItemS1.source=""
            }
        }

        function bellkey(str){
            if( str===16 )
                bell.source="LIFT/ui2/bell.png"
            else if( str===15 )
                bell.source= "LIFT/ui2/bell_dn.png"
        }

        Timer {
           id: timer;
           interval: 60000;
           running: false
           repeat: false;
           triggeredOnStart: false;
           onTriggered:{
                 sfu.tout();
           }
       }

        function state_c(str)
        {
                //console.log(str);                
            if(str<9)
                stetem1.text=dialog.laugok?ste[str]:ste_en[str]
        }

        function dir_c(num)
        {
            if(num===0)
                dirItem1.source = "LIFT/ui2/dir_up.png"
            else if(num===1)
                dirItem1.source = "LIFT/ui2/dir_dn.png"
            else if(num===2)
            {
                dirItem1.source = ""

                regnum=0;

                flrItemR1.source=""
                btn1.source = btn1.source0
                btn1.pressen = true

                flrItemR2.source=""
                btn2.source = btn2.source0
                btn2.pressen = true

                flrItemR3.source=""
                btn3.source = btn3.source0
                btn3.pressen = true

                flrItemR4.source=""
                btn4.source = btn4.source0
                btn4.pressen = true

                flrItemR5.source=""
                btn5.source = btn5.source0
                btn5.pressen = true

                flrItemR6.source=""
                btn6.source = btn6.source0
                btn6.pressen = true

                flrItemR7.source=""
                btn7.source = btn7.source0
                btn7.pressen = true

                flrItemR8.source=""
                btn8.source = btn8.source0
                btn8.pressen = true
            }
        }

       function flr_c(num)
       {
           flrItem1.source="LIFT/ui2/XL/"+flrn[num-1]+".png";
           //voice_c(num)
       }

       function vision_c(num)
       {
           dialog.visions=num
       }

     function voice_c(num)
     {
         if(!dialog.voiceok)
             return
         mediaplayer.stop()
         if(dialog.laugok)
         {
               if(num<10)
               {
                    mediaplayer.source="LIFT/MP3/"+voicen[num-1]+".mp3"
               }
               else if(num==90)
                    mediaplayer.source="LIFT/MP3/V90.mp3"
               else if(num==91)
                    mediaplayer.source="LIFT/MP3/V91.mp3"
               else if(num==92)
                    mediaplayer.source="LIFT/MP3/V92.mp3"
               else if(num==93)
                    mediaplayer.source="LIFT/MP3/V93.mp3"
         }
         else
         {
             if(num<10)
             {
                  mediaplayer.source="LIFT/MP3_EN/"+voicen[num-1]+".mp3"
             }
             else if(num==90)
                  mediaplayer.source="LIFT/MP3_EN/V90.mp3"
             else if(num==91)
                  mediaplayer.source="LIFT/MP3_EN/V91.mp3"
             else if(num==92)
                  mediaplayer.source="LIFT/MP3_EN/V92.mp3"
             else if(num==93)
                  mediaplayer.source="LIFT/MP3_EN/V93.mp3"
         }
         mediaplayer.play();
     }

        function regflr_c(num)
        {
            //console.log("register success!")
            var fg=0;
            if(num===1)
            {
                if(btn1.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                    flrItemR1.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                    btn1.source = btn1.source1
                    btn1.pressen = false
                }
            }
            else  if(num===2)
            {
                if(btn2.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                    flrItemR2.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                    btn2.source = btn2.source1
                    btn2.pressen = false
                }
            }
            else  if(num===3)
            {
                if(btn3.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR3.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn3.source = btn3.source1
                btn3.pressen = false
                }
            }
            else  if(num===4)
            {
                if(btn4.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR4.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn4.source = btn4.source1
                btn4.pressen = false
                }
            }
            else  if(num===5)
            {
                if(btn5.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR5.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn5.source = btn5.source1
                btn5.pressen = false
                }
            }
            else  if(num===6)
            {
                if(btn6.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR6.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn6.source = btn6.source1
                btn6.pressen = false
                }
            }
            else  if(num===7)
            {
                if(btn7.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR7.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn7.source = btn7.source1
                btn7.pressen = false
                }
            }
            else  if(num===8)
            {
                if(btn8.pressen)
                {
                    fg=1;
                    flrItemS1.source="";
                    regnum++;
                flrItemR8.source="LIFT/ui2/ML/"+flrn[num-1]+".png";
                btn8.source = btn8.source1
                btn8.pressen = false
                }
            }            

            if(regnum>2)
            {
                if(fg)
                {
                   txtscroll.stop();
                   txtscroll.start();
                }
            }
            else
            {
               txtscroll.stop();
                if(regnum==2)
                    flrItemRtmp.x=21
                else if(regnum==1)
                    flrItemRtmp.x=134
                else
                flrItemRtmp.x=0
            }

            if(fg)
                keypress(17)
        }

        function unregflr_c(num)
        {
           var fg=0;
           if(num===1)
           {
               if(!btn1.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR1.source=""
               btn1.source = btn1.source0
               btn1.pressen = true
               }
           }
           else  if(num===2)
           {
               if(!btn2.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR2.source=""
               btn2.source = btn2.source0
               btn2.pressen = true
                }
           }
           else  if(num===3)
           {
               if(!btn3.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR3.source=""
               btn3.source = btn3.source0
               btn3.pressen = true
                }
           }
           else  if(num===4)
           {
               if(!btn4.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR4.source=""
               btn4.source = btn4.source0
               btn4.pressen = true
                }
           }
           else  if(num===5)
           {
               if(!btn5.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR5.source=""
               btn5.source = btn5.source0
               btn5.pressen = true
                }
           }
           else  if(num===6)
           {
               if(!btn6.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR6.source=""
               btn6.source = btn6.source0
               btn6.pressen = true
                }
           }
           else  if(num===7)
           {
               if(!btn7.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR7.source=""
               btn7.source = btn7.source0
               btn7.pressen = true
                }
           }
           else  if(num===8)
           {
               if(!btn8.pressen)
               {
                   regnum--;
                   fg=1;

               flrItemR8.source=""
               btn8.source = btn8.source0
               btn8.pressen = true
                }
           }

            if(regnum>2)
            {
                 if(fg)
                 {
                       txtscroll.stop();
                       txtscroll.start();
                 }
            }
            else
            {
               txtscroll.stop();
                if(regnum==2)
                    flrItemRtmp.x=21
                else if(regnum==1)
                    flrItemRtmp.x=134
                else
               flrItemRtmp.x=0
            }
        }

}
